﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Evaluate.BLL;
using System.Text;
using System.Data;

public partial class Evaluate_AwardReportDAll : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "100103"))
        {
            //Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            BindYearList();
            BindDepartment();
            CreateTable();
        }
    }

    void BindDepartment()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        List<DepartmentInfo> list = Yipusoft.Account.BLL.Department.GetList(" departmentId like '%0" + departmentId.Substring(5, 1) + "0' and departmentId<>'13000" + departmentId.Substring(5, 1) + "0'");
        ddlDepartment.DataValueField = "DepartmentId";
        ddlDepartment.DataTextField = "ShortName";
        ddlDepartment.DataSource = list;
        ddlDepartment.DataBind();
        ddlDepartment.SelectedIndex = 0;
    }

    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }
    private string GetWhere()
    {
        string DepartmentID = ddlDepartment.SelectedValue;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr = "And SubString(DepartmentID,6,1) IN('1','2','3','4') And  SubString(DepartmentID,5,1) ='0'";
        }
        else
        {
            //总队人员
            if (DepartmentID.Substring(3, 2).Equals("00"))
            {
                ReturnStr = " And SubString(DepartmentID,6,1)='" + DepartmentID.Substring(5, 1) + "' And SubString(DepartmentID,5,1)='0'";
            }
            else
            {
                //如果属于支队司政后防
                if (DepartmentID.Substring(4, 1).Equals("0"))
                {
                    ReturnStr = " And Left(DepartmentID,6)= '" + DepartmentID.Substring(0, 6) + "'";
                }

            }
        }

        return ReturnStr;

    }

    private int GetGroupByLen()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        //是否是司政后防管理员
        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return 4;
        }
        else
        {
            return 6;
        }
    }

    private string GetDepartmentID()
    {
        string DepartmentID = ddlDepartment.SelectedValue;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(a.DepartmentID,2)=''" + DepartmentID.Substring(5, 1) + "0'' And ";

    }

    private string GetDepartmentIDChar()
    {
        string DepartmentID = ddlDepartment.SelectedValue;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(DepartmentID,2)='" + DepartmentID.Substring(5, 1) + "0' And ";

    }
    private void CreateTable()
    {
        string departmentId = ddlDepartment.SelectedValue;
        DataSet ds = SplitPage.ExecuteSQL("SELECT ShortName,DepartmentID FROM Account_Departments WHERE DepartmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' order By DepartmentID");
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ',SUM(CASE WHEN b.DepartmentId2 = ''' + DepartmentId + ''' THEN b.ItemScore END) AS D' + DepartmentId + '' FROM Account_Departments WHERE DepartmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' order By DepartmentID");
        sql.AppendLine("SET @SQL = 'SELECT a.AwardID, a.AwardName' + @SQL + ' FROM Awards a LEFT JOIN AwardItems b ON a.AwardID = b.AwardID WHERE a.departmentid like ''" + departmentId.Substring(0, 6) + "%'' and YEAR(a.AwardDate)=" + YearList.SelectedValue + " and flag=" + rbType.SelectedValue + " group BY a.AwardID, a.AwardName,a.TotalScore'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();
        tb.Caption = "<span>" + YearList.SelectedItem.Text + "工作奖惩加分情况统计表</span>";

        tb.CellPadding = 4;
        tb.CellSpacing = 0;
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();

        tc.Text = "";

        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.Text = "工作奖惩名称";
        tc.Style.Add("white-space", "nowrap");
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;


            tc.Style.Add("white-space", "nowrap");
            tc.Text = ds.Tables[0].Rows[i][0].ToString();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        int row = 1;
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = row.ToString();
            tc.Style.Add("white-space", "nowrap");
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;
            tc.Text = Award.GetAward((int)drv[0]).AwardName;
            tr.Cells.Add(tc);
            for (int i = 2; i <= col + 1; i++)
            {
                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Center;
                string score = "";
                if (drv[i] != DBNull.Value)
                    score = ((decimal)drv[i]).ToString("0");
                else
                    score = "0";
                tc.Text = "<a href=AwardViewD.aspx?AwardId=" + drv[0] + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + score + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }

        //添加合计列
        sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(max)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ',case when SUM(CASE WHEN b.DepartmentId= ''' + DepartmentID + ''' THEN b.ItemScore END) is null then 0 else SUM(CASE WHEN b.DepartmentId2 = ''' + DepartmentId+ ''' THEN b.ItemScore END) end  AS D' + DepartmentId + '' FROM Account_Departments WHERE DepartmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' Group By DepartmentID");
        sql.AppendLine("SET @SQL = 'SELECT 0' + @SQL + ' FROM Awards a LEFT JOIN AwardItems b ON a.AwardID = b.AwardID WHERE a.departmentId like ''" + departmentId.Substring(0, 6) + "%''and flag=" + rbType.SelectedValue + " and YEAR(a.AwardDate)=" + YearList.SelectedValue + "'");
        sql.AppendLine("EXEC(@SQL)");
        dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        tr = new TableRow();
        tr.CssClass = "grid_item";
        tc = new TableCell();
        tc.Text = "";

        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.Text = "合计";
        tr.Cells.Add(tc);
        for (int i = 1; i <= col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Style.Add("white-space", "nowrap");
            if (dv[0][i] != DBNull.Value)
            {
                tc.Text = (decimal.Parse(dv[0][i].ToString())).ToString("0");
            }
            else
            {
                tc.Text = "0";
            }
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        PlaceHolder.Controls.Add(tb);

        BindChart();
    }
    private void BindChart()
    {
        string departmentId = ddlDepartment.SelectedValue;
        StringBuilder sql = new StringBuilder();
        sql.Append("select DepartmentID,shortName, ");
        sql.Append("(select sum(itemscore) from Awarditems where Awardid in (select Awardid from Awards where flag=" + rbType.SelectedValue + " and departmentId like '" + departmentId.Substring(0, 6) + "%' and year(Awarddate)=" + YearList.SelectedValue + ") and departmentid2=account_departments.departmentid) as score ");
        sql.Append("from account_departments where account_departments.departmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' Group by DepartmentID,shortName order by score desc");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        UserChart.Titles["Title"].Text = YearList.SelectedItem.Text + "各单位工作奖惩加分情况统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        dv.Sort = " Score asc ";
        UserChart.Series["加分"].Points.DataBindXY(dv, "shortName", dv, "Score");

        UserChart.Series[0].Type = Dundas.Charting.WebControl.SeriesChartType.Bar;
    }
    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void rbType_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
}
